// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (c) 2023 Yevhen Babiichuk (DustDFG)

#include <DotSort/VectorTubeSetBuilder.hpp>

#include <cstdint>

#include <DotSort/GameRules/TubeSet.hpp>
#include <DotSort/GameRules/Tube.hpp>

VectorTubeSetBuilder::VectorTubeSetBuilder(int8_t tubeSize, uint8_t tubeCount, std::vector<uint8_t> dots)
	: TubeSetBuilder(tubeSize, tubeCount)
	, dots(dots)
{}

void VectorTubeSetBuilder::fillInstance()
{
	for(uint8_t i = 0; i < this->tubeCount - 2; ++i)
	{
		for(uint8_t j = 0; j < this->tubeSize; ++j)
		{
			this->push(i, dots[(i * this->tubeSize) + j]);
		}
	}
}

VectorTubeSetBuilder* VectorTubeSetBuilder::getRandomInstance()
{
	std::random_device dev;
	std::mt19937 rng(dev());
	std::uniform_int_distribution<std::mt19937::result_type> distTubeSize(4,6);
	std::uniform_int_distribution<std::mt19937::result_type> distFilledTubeCount(5,12);

	uint8_t tubeSize = distTubeSize(rng);
	uint8_t filledTubeCount = distFilledTubeCount(rng);

	std::vector<uint8_t> dots;
	dots.reserve(filledTubeCount * tubeSize);

	for(uint8_t i = 0; i < filledTubeCount; ++i)
	{
		for(uint8_t j = 0; j < tubeSize; ++j)
		{
			dots.push_back(i);
		}
	}

	std::shuffle(dots.begin(), dots.end(), rng);

	return new VectorTubeSetBuilder(tubeSize, filledTubeCount + 2, dots);
}
